Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-96030: IsolatedAsyncioTestCase: don't create asyncio runner for skipped tests #96031

Closed
wants to merge 2 commits into from

Conversation

tiran
Copy link
Member

@tiran tiran commented Aug 16, 2022

@tiran tiran added the needs backport to 3.11 only security fixes label Aug 16, 2022
@tiran tiran requested review from asvetlov and zware August 16, 2022 18:04
def debug(self):
self._setupAsyncioRunner()
super().debug()
self._tearDownAsyncioRunner()
Copy link
Contributor

@graingert graingert Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this used to not be called on an exception in debug() you probably want a test for this new behavior ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think you fixed this #96021

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "this"? The function call self._tearDownAsyncioRunner()? It still does not get called when the setup or test case fails. It now gets called when asyncTearDown or tearDown fail with an exception. I think it's an acceptable deviation. Exceptions in tear down code are rare and it's ok to clean up the runner on error.

Copy link
Contributor

@graingert graingert Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right but it used to not get called when debug failed, and now it does? So I think you fixed two bugs

self._asyncioTestContext.run(self.setUp)
self._callAsync(self.asyncSetUp)
except BaseException:
# _callTearDown is not called when _callSetUp fails.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that doCleanups() is called in run() if _callSetUp() fails. It will try to call callbacks after closing the runner.

@serhiy-storchaka
Copy link
Member

This is an interesting approach, and I would prefer a code that overrides methods like _callSetUp() over the code that overrides run(), but there are problems with current code. I think that simpler #96033 should fix the original issue.

@tiran
Copy link
Member Author

tiran commented Aug 16, 2022

My approach addresses the fact that the current code does too much when a test is skipped.

Let's merge your simple fix to unblock main and somebody else can take over my PR fix the code properly.

@gvanrossum
Copy link
Member

How does this compare to #96033? It seems that is meant to fix the same issue.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to what Serhiy and Thomas said.

try:
self._asyncioTestContext.run(self.setUp)
self._callAsync(self.asyncSetUp)
except BaseException:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I don't like catching BaseException here. It seems to only coincidentally fix the underlying issue -- which IMO is that the asyncio runner is set up even for skipped tests.

And the duplication of calling _tearDownAsyncioRunner both here and in _callTearDown also jars.

Copy link
Contributor

@graingert graingert Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes this means if you have an IsolatedAsyncioTestCase with only skipped tests it means set_event_loop(None) is called when you might not have expected it to be

@gvanrossum
Copy link
Member

This PR appears doomed. @tiran want to just close it?

@serhiy-storchaka
Copy link
Member

The original issue was fixed, and it is a proper solution.

The code can be made more "elegant" if use the abstarct idea of this PR. I tried to implement it, but the simple and clear implementation of it free from the flaws of this PR suffered from two other issues:

  1. It breaks the case when doCleanup() is called in tesrDown(). This case is not covered by IsolatedAsyncioTestCase, but it happens with non-asynchronous tests, and it can happen with third party asynchronous tests,
  2. It creates a reference loop, which make the problem of cleaning up of abandoned IsolatedAsyncioTestCase instance worse.

It is possible to fix these two issues also, but the resulting code is far from "elegant". So for now I'm happy with the current code.

@itamaro
Copy link
Contributor

itamaro commented Jul 26, 2023

So for now I'm happy with the current code.

So this issue is good to close?

@gvanrossum
Copy link
Member

Closing per Serhiy's recommendation.

@gvanrossum gvanrossum closed this Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants